<?php
/**
 * Created by JetBrains PhpStorm.
 * User: jonatan
 * Date: 1/31/12
 * Time: 1:08 PM
 * To change this template use File | Settings | File Templates.
 */
class TopThreePosts extends CI_Model
{
    protected $topThree = array();

    public function validateAndAdd($post)
    {
        if ($this->validate($post)) {
            if(sizeof($this->topThree) < 3)
            {
                $this->topThree[] = $post;
            }
            else
            {
                $this->topThree[0] = $post;
            }
        }
        usort($this->topThree, array($this,"comparePost"));
    }

    public function getTopThree()
    {
        return $this->topThree;
    }

    private function validate($post)
    {
        if (sizeof($this->topThree) < 3)
            return true;
        if ($post->getReadCount() > $this->topThree[0]->getReadCount())
            return true;
        return false;
    }


    public function comparePost($post1, $post2)
    {
            return $post1->getReadCount() - $post2->getReadCount();
    }
}
